from tkinter import *
import datetime
import time
import os
import threading

#设置日志
def log_info():
    return str(datetime.datetime.now()) + '：[info] '
def log_error():
    return str(datetime.datetime.now()) + '：[error] '
def log_warning():
    return str(datetime.datetime.now()) + '：[warning] '

'''
def thread(self):
        threading.Thread(target=gui_thread).start()

def gui_thread():
    root = Tk()
    root.title('apple')
    root.geometry('800x600')
    root.resizable(False, False)

    App(root)
    root.mainloop()
'''
class App:
    def __init__(self, root):
        self.frame = Frame(root)        #最主要的一环
        self.frame.pack()
        self.buju()

    def buju(self):
        fm = Frame(root)
        scroll = Scrollbar(fm, orient=VERTICAL)
        self.path_text = Text(fm, font=('宋体', 11),
                              bg='#FFFEEE', fg='green',
                              width=95, height=23,
                              spacing1=5)
        #self.path_text.pack(fill=X, padx=1, pady=10, side=TOP)
        path = Label(root, text='nmon路径：', font=('宋体', 14))
        self.path_entry = Entry(root, show=None, font=('Arial', 12), width=35)

        scroll.config(command=self.path_text.yview)
        self.path_text.config(yscrollcommand=scroll.set)
        scroll.pack(side=RIGHT, fill=Y)
        self.path_text.pack(fill=X, padx=1, pady=10, side=TOP)
        fm.pack()  # 添加一个frame，text与滚动条进行关联

        path.place(x=20, y=510)
        self.path_entry.place(x=150, y=510)

    '''
    #被调用
    #@staticmethod
    def say_hi(self):
        self.path_text.insert(END, 'Hello World!\n')
        #self.path_text.insert(END, 'Hello World,2\n')

    def say_test(self):
        self.path_text.insert(END, '开始log日志打印，very good！！\n')
    '''
    #@staticmethod
    def ok(self):

        nmonpath = self.path_entry.get()
        #print(nmonpath)
        #path_text = nmonpath
        # 判断输入的路径是否合法
        if ('c:\\' not in nmonpath) and ('d:\\' not in nmonpath) and ('e:\\' not in nmonpath) and (
                'f:\\' not in nmonpath) \
                and ('C:\\' not in nmonpath) and ('D:\\' not in nmonpath) and ('E:\\' not in nmonpath) and (
                'F:\\' not in nmonpath):
            self.path_text.insert(END, log_error() + '请输入正确的windows路径。\n')
            root.update()  # 更新输入的内容
            self.path_text.insert(END, log_warning() + '请输入正确的路径。\n')  # 打印日志
        else:
            self.path_text.insert(END, log_info() + '您输入的路径是：' + nmonpath + '\n')
            self.path_text.see(END)
            self.path_text.focus_force()  # 获取光标
            for i in range(10):
                self.path_text.insert(END, log_info() + 'range的结果：' + str(i) + '\n')
                root.update()
                self.path_text.see(END)  # 在text控件中自动滚动显示log内容
                time.sleep(1)
                # i = i + 1
        return nmonpath

    # 打开结果目录
    def result(self):
        # path_text, path_entry = buju()
        nmonpath = self.path_entry.get()
        if ('c:\\' not in nmonpath) and ('d:\\' not in nmonpath) and ('e:\\' not in nmonpath) and (
                'f:\\' not in nmonpath) \
                and ('C:\\' not in nmonpath) and ('D:\\' not in nmonpath) and ('E:\\' not in nmonpath) and (
                'F:\\' not in nmonpath):
            self.path_text.insert(END, log_info() + '请输入正确的windows路径。\n')
            self.path_text.see(END)
            root.update()
            time.sleep(0.5)
            self.path_text.insert(END, log_warning() + '请输入正确的路径。\n')
            self.path_text.see(END)
        else:
            root.update()
            self.path_text.insert(INSERT, log_info() + '结果路径：' + nmonpath + '\\result' + '\n')
            self.path_text.see(END)
            os.system('start explorer ' + nmonpath + '\\result')  # 打开分析结果文件夹
            self.path_text.insert(INSERT, log_info() + '正在nmon分析结果文件夹，请稍等片刻' + '\n')
            self.path_text.see(END)

    # 中止nmon分析过程，考虑使用多线程
    def kill1(self):
        self.path_text.insert(END, log_error() + 'nmon分析过程被人为中断，请重新核对分析过程是否正确，谢谢。\n请重新启动\n')
        self.path_text.see(END)

    def main(self):
        '''
        #self.say_test()
        self.button = Button(self.frame, text='打印', command=self.say_hi)        #调用say_hi()函数
        self.button1 = Button(self.frame, text='日志', command=self.say_test)        #调用say_hi()函数

        self.button1.pack(side=RIGHT)
        self.button.pack(side=LEFT)
        '''

        path_entrybt = Button(root, text='nmon分析', command=self.ok, font=('宋体', 11), fg='green')
        resultbt = Button(root, text='打开结果目录', command=self.result, font=('宋体', 11), fg='green')
        killbt = Button(root, text='中止分析', command=self.kill1, font=('宋体', 11), fg='green')

        path_entrybt.place(x=490, y=504)
        resultbt.place(x=580, y=504)
        killbt.place(x=700, y=504)


if __name__ == '__main__':
    #main()
    root = Tk()
    root.title('My Little Apple')
    root.geometry('800x600')
    root.resizable(False, False)

    App(root).main()
    root.mainloop()

